package com.ruoyi.web.controller.blog.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.blog.domain.TbBlogCategory;
import com.ruoyi.blog.service.ITbBlogCategoryService;
import com.ruoyi.common.utils.uuid.IdWorker;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章分类Controller
 *
 * @author ruoyi
 * @date 2024-03-08
 */
@RestController
@RequestMapping("/ruoyi/category")
public class TbBlogCategoryController extends BaseController {
    @Autowired
    private ITbBlogCategoryService tbBlogCategoryService;

    @PreAuthorize("@ss.hasPermi('ruoyi:category:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll() {
        LambdaQueryWrapper<TbBlogCategory> queryWrapper = new LambdaQueryWrapper<>();
        List<TbBlogCategory> list = tbBlogCategoryService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询文章分类列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbBlogCategory tbBlogCategory) {
        startPage();
        List<TbBlogCategory> list = tbBlogCategoryService.selectTbBlogCategoryList(tbBlogCategory);
        return getDataTable(list);
    }

    /**
     * 导出文章分类列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:category:export')")
    @Log(title = "文章分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbBlogCategory tbBlogCategory) {
        List<TbBlogCategory> list = tbBlogCategoryService.selectTbBlogCategoryList(tbBlogCategory);
        ExcelUtil<TbBlogCategory> util = new ExcelUtil<TbBlogCategory>(TbBlogCategory.class);
        util.exportExcel(response, list, "文章分类数据");
    }

    /**
     * 获取文章分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") String categoryId) {
        return success(tbBlogCategoryService.selectTbBlogCategoryByCategoryId(categoryId));
    }

    /**
     * 新增文章分类
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:category:add')")
    @Log(title = "文章分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbBlogCategory tbBlogCategory) {
        return toAjax(tbBlogCategoryService.insertTbBlogCategory(tbBlogCategory));
    }

    /**
     * 修改文章分类
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:category:edit')")
    @Log(title = "文章分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbBlogCategory tbBlogCategory) {
        return toAjax(tbBlogCategoryService.updateTbBlogCategory(tbBlogCategory));
    }

    /**
     * 删除文章分类
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:category:remove')")
    @Log(title = "文章分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable String[] categoryIds) {
        return toAjax(tbBlogCategoryService.deleteTbBlogCategoryByCategoryIds(categoryIds));
    }
}
